'''
自定义线程模仿死锁过程，解锁
1、自定义线程也是继承Thread然后重写run方法
死锁
'''
from threading import Thread,Lock
import time

lockA = Lock()
lockB = Lock()

class MyThread1(Thread):
	def run(self):
		if lockA.acquire():
			print(self.name + '获取了A锁')
			time.sleep(0.1)
			if lockB.acquire(timeout=5):
				print(self.name + '获取了B锁，原来还有A锁')
			lockA.release()
	
class MyThread2(Thread):
	def run(self):
		if lockB.acquire():
			print(self.name + '获取了B锁')
			time.sleep(0.1)
			if lockA.acquire(timeout=5):
				print(self.name + '获取了A锁，原来还有B锁')
			lockB.release()


if __name__ == '__main__':
	t1 = MyThread1()
	t2 = MyThread2()
	
	t1.start()
	t2.start()